import 'package:flutter/material.dart';
import 'package:lib_ui/gen/colors.gen.dart';
import 'package:lib_ui/lib_ui.dart';

import '../utils/ld_font.dart';

enum MainButtonType { normal, primary, accent, accentOutline }

class MainButton extends StatelessWidget {
  final String text;
  final bool enabled;
  final MainButtonType type;
  EdgeInsetsGeometry? padding;
  Color? backgroundColor;
  BorderSide? borderSide;
  Color? textColor;
  final VoidCallback onClick;
  final FontWeight fontWeight;
  double fontSize;

  MainButton({
    Key? key,
    required this.text,
    this.enabled = true,
    this.type = MainButtonType.normal,
    this.padding,
    this.backgroundColor,
    this.borderSide,
    this.textColor,
    required this.onClick,
    this.fontWeight = FontWeight.w400,
    this.fontSize = 15,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: enabled ? onClick : null,
      style: TextButton.styleFrom(
        fixedSize: const Size(double.infinity, double.infinity),
        shape: StadiumBorder(
          side: borderSide != null
              ? borderSide!
              : enabled && type == MainButtonType.normal
                  ? const BorderSide(width: 1, color: Color(0xFFC2C2C2))
                  : BorderSide.none,
        ),
        padding: padding ?? const EdgeInsets.symmetric(vertical: 11,horizontal: 13),
        backgroundColor: _backgroundColor(),
      ),
      child: Text(
        text,
        style: LDFont.medium(fontWeight: fontWeight,fontSize: fontSize, color: _textColor()),
      ),
    );
  }

  Color _backgroundColor() {
    if (!enabled) {
      if(backgroundColor != null){
        return backgroundColor!.withOpacity(0.5);
      }else {
        return const Color(0xFFC8C8C8);
      }
    }
    if (backgroundColor != null) return backgroundColor!;

    switch (type) {
      case MainButtonType.normal:
        return Colors.white;
      case MainButtonType.primary:
        return ColorName.primary;
      case MainButtonType.accent:
        return Colors.white;
      case MainButtonType.accentOutline:
        return ColorName.accent;
        break;
    }
  }

  Color _textColor() {
    if (!enabled) return Colors.white;
    if (textColor != null) return textColor!;

    switch (type) {
      case MainButtonType.normal:
        return ColorName.textPrimary;
      case MainButtonType.primary:
        return Colors.white;
      case MainButtonType.accent:
        return ColorName.accent;
      case MainButtonType.accentOutline:
        return Colors.white;
    }
  }
}
